/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.imr.support;

import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    /**
     * 
     */
    static {
        try { 
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }catch (Exception e) {
	        //Log the exception. 
	        System.err.println("Initial SessionFactory creation failed." + e);
	        throw new ExceptionInInitializerError(e);
	    } catch (Throwable ex) {
            //Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        } 
    }
    
    
    public static Session getSession() {
        return sessionFactory.openSession();
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
